<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">
                        <a-form-item label="货物名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-select v-decorator="['huaxuepinmingcheng']" @change="handleChange"
                                placeholder="请输入货物名称">
                                <a-select-option v-for="item in this.huaxuepinmingchengList"
                                    :value="item" :key="item">
                                    {{ item }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-select v-decorator="['cangkumingcheng']" @change="handleChange1"
                                placeholder="请输入仓库名称">
                                <a-select-option v-for="item in this.cangkumingList" :value="item"
                                    :key="item">
                                    {{ item }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

<!--                    <a-col :span="24">
                        <a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-select v-decorator="['picihao']" placeholder="请输入批次号">
                                <a-select-option v-for="item in this.picihaoList" :value="item"
                                    :key="item">
                                    {{ item }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>-->
                    <a-col :span="24">
                        <a-form-item label="实际数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['shijizhongliang']"
                                placeholder="请输入实际数量"></a-input>
                        </a-form-item>
                    </a-col>
                    <!-- <a-col :span="24">
            <a-form-item label="理论重量(克)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['lilunzhongliang']" placeholder="请输入理论重量"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="差异重量(克)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['chayizhongliang']" placeholder="请输入差异重量"  ></a-input>
            </a-form-item>
          </a-col> -->
                    <!-- <a-col :span="24">
            <a-form-item label="原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['yuanyin']" placeholder="请输入原因"  ></a-input>
            </a-form-item>
          </a-col> -->
                    <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
    name: 'YzdPandianxiangqingForm',
    components: {
    },
    props: {
        //流程表单data
        formData: {
            type: Object,
            default: () => { },
            required: false
        },
        //表单模式：true流程表单 false普通表单
        formBpm: {
            type: Boolean,
            default: false,
            required: false
        },
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            form: this.$form.createForm(this),
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            validatorRules: {
            },
            url: {
                selectByHuaMing: "",
                picihao: "/yzd_zaikuguanli/zaikumingxi/getPicihao",
                cangkumingcheng: "/yzd_jichuxinxi/yzdZhucuncangkuxinxi/huoQuCangKu",
                select: "/yzd_jichuxinxi/yzdWeixianfeiwuxinxi/list",
                add: "/yzd_pandianmingxi/yzdPandianxiangqing/add",
                edit: "/yzd_pandianmingxi/yzdPandianxiangqing/edit",
                queryById: "/yzd_pandianmingxi/yzdPandianxiangqing/queryById"
            },
            huaxuepinmingchengList: [],
            cangkumingList: [],
            picihaoList: [],
            shuaxuepinmingcheng: '',
            scangkumingcheng: '',
            huaxuepinmingcheng: '',
        }
    },
    computed: {
        formDisabled() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return false
                }
                return true
            }
            return this.disabled
        },
        showFlowSubmitButton() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return true
                }
            }
            return false
        }
    },
    created() {
        //如果是流程中表单，则需要加载流程表单data
        this.showFlowData();
        this.getCangkumingcheng();
        this.getselect();
    },
    methods: {
        getListByhuaming(res) {
            getAction(this.url.selectByHuaMing, res).then(res => {
                console.log(res);
            })
        },



        getCangkumingcheng() {
            getAction(this.url.cangkumingcheng).then((res) => {
                console.log(res.result);
                res.result.forEach(element => {
                    this.cangkumingList.push(element.cangkumingcehng)
                    console.log(element.cangkumingcehng);
                });
            })
        },


        getselect() {
            getAction(this.url.select).then(res => {

                res.result.records.forEach(element => {
                    this.huaxuepinmingchengList.push(element.huaxuepinmingcheng)
                }
                )
            })
        },
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'cangkumingcheng', 'huaxuepinmingcheng', 'picihao', 'shijizhongliang', 'lilunzhongliang', 'chayizhongliang', 'yuanyin'))
            })
        },
        //渲染流程表单数据
        showFlowData() {
            if (this.formBpm === true) {
                let params = { id: this.formData.dataId };
                getAction(this.url.queryById, params).then((res) => {
                    if (res.success) {
                        this.edit(res.result);
                    }
                });
            }
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    let formData = Object.assign(this.model, values);
                    console.log("表单提交数据", formData)
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }

            })
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'cangkumingcheng', 'huaxuepinmingcheng', 'picihao', 'shijizhongliang', 'lilunzhongliang', 'chayizhongliang', 'yuanyin'))
        },
        handleChange(value) {

            this.shuaxuepinmingcheng = value

            if (this.scangkumingcheng.length != 0) {

                this.getpicihao()
            }
        },
        handleChange1(value) {

            this.scangkumingcheng = value
            if (this.shuaxuepinmingcheng.length != 0) {
                this.getpicihao()
            }
        },
        getpicihao() {
            console.log("进入批次号方法");
            var re = {
                "cangkumingchedng": this.scangkumingcheng,
                "huaxuepinmingcheng": this.shuaxuepinmingcheng
            }

            if (cangkumingcheng != null && huaxuepinmingcheng != null) {
                getAction(this.url.picihao, re).then((res) => {
                    console.log(res);
                    this.picihaoList = res.result
                })
            }
        },
    }
}
</script>